package com.admin.cartAndOrder.controller;

import com.admin.cartAndOrder.pojo.MobileEndOrder;
import com.admin.cartAndOrder.pojo.OrderInfo;
import com.admin.cartAndOrder.service.MobileEndOrderService;
import com.admin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/order"})
@CrossOrigin
public class MobileEndOrderController {
    @Autowired
    private MobileEndOrderService orderService;

    /**
     * 生成订单
     * /order/addOrder
     * @param orderInfo 需要的订单信息
     * orderInfo具体属性如下：
     *     private String userNum;//用户编号
     *     private Integer[] cartIds;//选中的购物项id
     *     private String name;//收获姓名
     *     private String tel;//收获电话
     *     private String address;//收货地址
     *     private Long discountId;//使用到的优惠券id（仅限一张）
     *     private Double totalPrice;//总金额
     *     private Double realPrice;//实付金额
     * @return 回传操作结果
     */
    @RequestMapping({"addOrder"})
    public R addOrder(@RequestBody OrderInfo orderInfo) {
        MobileEndOrder order = this.orderService.createOrder(orderInfo);
        boolean save = this.orderService.save(order);
        return save ? R.ok() : R.error();
    }

    /**
     * 传入订单编号查询订单
     * /order/orderInfo/{orderId}
     * @param orderId 订单编号
     * @return 回传订单信息 在data的order属性中
     */
    @RequestMapping({"orderInfo/{orderId}"})
    public R selectOrder(@PathVariable("orderId") String orderId) {
        return R.ok().data("order", this.orderService.getOrderByOrderId(orderId));
    }

    /**
     * /order/allOrderInfos/{userNum}
     * 传入用户编号查看当前用户所有订单
     * @param userNum 用户编号
     * @return 回传订单集合 在data的orders属性中
     */
    @RequestMapping({"allOrderInfos/{userNum}"})
    public R selectOrders(@PathVariable("userNum") String userNum) {
        return R.ok().data("orders", this.orderService.getAllOrders(userNum));
    }
}

